﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quality.spc
{
    public partial class getdata : Form
    {
        public getdata()
        {
            InitializeComponent();
            DB.getcombobox(shift, "shift", "num", "shiftname");
            DB.getcombobox(typenum, "testtypes", "num", "typename");
            DB.getcombobox(worknum, "workcenters", "num", "workname");
        }

        private void typenum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (typenum.SelectedIndex != -1)
            {
                DataTable dt = sqlHelper.ExecuteDataTable("select num,itemname from testitems a left join assigneditems b on a.num=b.itemnum where b.typenum='" + typenum.SelectedValue + "'");
                itemnum.DataSource = dt;
                itemnum.DisplayMember = "itemname";
                itemnum.ValueMember = "num";
                itemnum.SelectedIndex = -1;

            }
        }

        private void itemnum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (itemnum.SelectedIndex != -1)
            {
                DataTable dt = sqlHelper.ExecuteDataTable("select num,indname from testindicators a left join assignedindicators b on a.num=b.indnum where b.itemnum='" + itemnum.SelectedValue + "'");
                indnum.DataSource = dt;
                indnum.DisplayMember = "indname";
                indnum.ValueMember = "num";
                indnum.SelectedIndex = -1;

            }
        }

        private void worknum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (worknum.SelectedIndex != -1)
            {

                DataTable dt = sqlHelper.ExecuteDataTable("select num,eqname from equipments  where worknum='" + worknum.SelectedValue + "'");
                eqnum.DataSource = dt;
                eqnum.DisplayMember = "eqname";
                eqnum.ValueMember = "num";
                eqnum.SelectedIndex = -1;
            }
        }
        public event TransfDelegatedt TransfEventdt;
        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(workdate.Text) || string.IsNullOrEmpty(workdate1.Text))
            {
                MessageBox.Show("生产日期不能为空！");
            }

            string s = "";

            if (!string.IsNullOrEmpty(num.Text))
            {
                s = s + " and a.num='" + num.Text.Trim() + "' ";

            }
            if (!string.IsNullOrEmpty(shift.Text))
            {
                s = s + " and a.shift='" + shift.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(pronum.Text))
            {
                s = s + " and a.pronum like '%" + pronum.Text.Trim() + "%' ";

            }
            if (!string.IsNullOrEmpty(proname.Text))
            {
                s = s + " and a.proname like '%" + proname.Text.Trim() + "%' ";

            }

            if (!string.IsNullOrEmpty(batch.Text))
            {
                s = s + " and a.batch like '%" + batch.Text.Trim() + "%' ";

            }
            if (!string.IsNullOrEmpty(typenum.Text))
            {
                s = s + " and a.typenum='" + typenum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(itemnum.Text))
            {
                s = s + " and a.itemnum='" + itemnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(indnum.Text))
            {
                s = s + " and a.indnum='" + indnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(eqnum.Text))
            {
                s = s + " and a.eqnum='" + eqnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(worknum.Text))
            {
                s = s + " and c.num='" + worknum.SelectedValue + "' ";

            }

            DataTable dt = sqlHelper.ExecuteDataTable("select f.indname,a.testvalue,f.indvalue,a.num,a.rownum,a.workdate,a.shift,a.pronum ,a.proname,a.batch,c.workname,b.eqname,d.typename,e.itemname from records a left join equipments b on a.eqnum=b.num left join workcenters c on b.worknum=c.num  left join testtypes d on a.typenum=d.num  left join testitems e on a.itemnum=e.num left join  testindicators f on a.indnum=f.num where workdate>='" + workdate.Value + "' and workdate<='" + workdate1.Value + "' " + s + "");

            TransfEventdt(dt);
            this.Close();
        }
    }
}
